var express = require("express");
var router = express.Router();
var banner = require("../../models/admin/banner.js");
//添加
router.post("/addBanner" , function(req , res){
  var body = req.body,
      data = {
        title:body.title,
        img:body.img,
        href:body.href,
        sort:body.sort,
        releaseTime:new Date().getTime()
      };
  if(!data.img){
    return res.json({
      state:"error",
      message:"banner图片不能为空"
    });
  };
  //判断是新加还是编辑
  if(body._id){
    banner.update(body._id , data , function(error , data){
      if(error){
        return res.json({
          state:"error",
          message:error
        });
      };
      res.json({
        state:"ok",
        message:"编辑成功"
      });
    });
  }else{
    banner.addBanner(data , function(error , data){
      if(error){
        return res.json({
          state:"error",
          message:"添加失败"
        });
      };
      res.json({
        state:"ok",
        message:"添加成功"
      });
    });
  };
});
//获取
router.get("/getBanner" , function(req , res){
  var data = {};
  req.query.id && (data._id = req.query.id);
  banner.getBanner(data , function(error , data){
    if(error){
      return res.json({
        state:"error",
        message:error
      });
    }else{
      return res.json({
        state:"ok",
        banner:data
      });
    };
  });
})
module.exports = router;
